<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
    <head>
        <th:block th:include="include :: header('课程考试题列表')"/>
    </head>
    <body class="gray-bg">
        <div class="container-div">
            <div class="row">
                <div class="col-sm-12 search-collapse">
                    <form id="formId">
                        <div class="select-list">
                            <ul>
                                <li class="col-sm-3">
                                    <label>所属课程：</label>
                                    <div>
                                        <div class="input-group">
                                            <input id="treeId" name="courseDetailId" type="hidden"
                                                   th:value="${courseDetail?.id}"/>
                                            <input class="form-control" type="text" onclick="selectManageTree()"
                                                   id="treeName"
                                                   readonly="true" th:value="${courseDetail?.courseName}">
                                            <span class="input-group-addon"><i class="fa fa-search"></i></span>
                                        </div>
                                    </div>
                                </li>
                                <li>
                                    <label>试题标题：</label>
                                    <input type="text" name="examTitle"/>
                                </li>

                                <li>
                                    <label>是否启用：</label>
                                    <select name="activeStatus">
                                        <option value="">所有</option>
                                        <option value="1">启用</option>
                                        <option value="0">未启用</option>
                                    </select>
                                </li>
                                <li>
                                    <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                            class="fa fa-search"></i>&nbsp;搜索</a>
                                    <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                            class="fa fa-refresh"></i>&nbsp;重置</a>
                                </li>
                            </ul>
                        </div>
                    </form>
                </div>

                <div class="btn-group-sm" id="toolbar" role="group">
                    <a class="btn btn-success" onclick="add()" shiro:hasPermission="biz:exam:add">
                        <i class="fa fa-plus"></i> 添加
                    </a>
<!--                    <a class="btn btn-primary single disabled" onclick="edit()"-->
<!--                       shiro:hasPermission="biz:exam:edit">-->
<!--                        <i class="fa fa-edit"></i> 修改-->
<!--                    </a>-->
                    <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()"
                       shiro:hasPermission="biz:exam:remove">
                        <i class="fa fa-remove"></i> 删除
                    </a>
                    <!--                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="biz:exam:export">-->
                    <!--                    <i class="fa fa-download"></i> 导出-->
                    <!--                </a>-->
                </div>
                <div class="col-sm-12 select-table table-striped">
                    <table id="bootstrap-table"></table>
                </div>
            </div>
        </div>
        <th:block th:include="include :: footer"/>
        <script th:inline="javascript">
            var editFlag   = [[${@permission.hasPermi('biz:exam:edit')}]];
            var removeFlag = [[${@permission.hasPermi('biz:exam:remove')}]];
            var prefix     = ctx + "biz/exam";

            $(function () {
                var options = {
                    url: prefix + "/list",
                    createUrl: prefix + "/add/{id}",
                    updateUrl: prefix + "/edit/{id}",
                    removeUrl: prefix + "/remove",
                    exportUrl: prefix + "/export",
                    modalName: "课程考试题",
                    columns: [{
                        checkbox: true
                    },
                        {
                            field: 'id',
                            title: 'id',
                            visible: false
                        },
                        {
                            field: 'subjectName',
                            title: '学科'
                        }, {
                            field: 'courseName',
                            title: '所属课程'
                        }, {
                            field: 'courseDetailTitle',
                            title: '课程内容'
                        },
                        {
                            field: 'examTitle',
                            title: '试题标题'
                        },
                        {
                            field: 'originText',
                            title: '试题文本',
                            visible: false
                        },
                        {
                            field: 'examContent',
                            title: '试题内容',
                            visible: false
                        },
                        {
                            field: 'orderNum',
                            title: '显示顺序'
                        },
                        {
                            field: 'activeStatus',
                            title: '是否启用',
                            formatter: function (value, row, index) {
                                if (0 === +value) {
                                    return '<span class="badge badge-danger">未启用</span>'
                                }
                                return '<span class="badge badge-primary">启用</span>'
                            }
                        },
                        {
                            title: '操作',
                            align: 'center',
                            formatter: function (value, row, index) {
                                var actions = [];
                                actions.push(
                                    '<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="preview(\'' + row.id + '\')"><i class="fa fa-edit"></i>预览</a> ');
                                actions.push(
                                    '<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                                actions.push(
                                    '<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                                return actions.join('');
                            }
                        }]
                };
                $.table.init(options);
            });

            function selectManageTree() {
                var options = {
                    title: '课程选择',
                    width: "380",
                    url: prefix + "/selectManageTree/" + $("#treeId").val(),
                    callBack: doSubmit
                };
                $.modal.openOptions(options);
            }

            function doSubmit(index, layero) {
                var tree = layero.find("iframe")[0].contentWindow.$._tree;
                if (checkL4(tree)) {
                    var body = layer.getChildFrame('body', index);
                    $("#treeId").val(+body.find('#treeId').val() + 100000);
                    $("#treeName").val(body.find('#treeName').val());
                    console.log('courseDetailId = ' + $("#treeId").val())
                    layer.close(index);
                }
            }

            function checkL4(_tree) {
                var nodes = _tree.getSelectedNodes();
                if (nodes.length == 0) {
                    $.modal.msgError("请选择节点后提交");
                    return false;
                }
                for (var i = 0; i < nodes.length; i++) {
                    if (nodes[i].level != 3) {
                        $.modal.msgError("数据不正确，请选择第四级菜单");
                        return false;
                    }

                }
                return true;
            }

            function add() {
                // $.operate.addTab($("#treeId").val())
                $.modal.openFull("新增" , ctx + "biz/exam/add/" + $("#treeId").val());
            }

            function edit(id) {
                // $.operate.addTab($("#treeId").val())
                $.modal.openFull("编辑" , ctx + "biz/exam/edit/" + id);
            }

            function preview(id) {
                // $.operate.addTab($("#treeId").val())
                $.modal.open("预览试题" , ctx + "biz/exam/preview/" + id);
            }
        </script>
    </body>
</html>